import {
  addAudit,
  getApplication,
  releaseCourse,
  getAudit,
  getAllAudit,
  updateAuditStatus,
  deleteAudit,
  deleteApply,
  getApplyResult,
  getStatistics
} from '@/api/audit'
import type { application } from '@/models/application'

export async function GetApplication(): Promise<application[]> {
  const applicationList: application[] = []
  const res = (await getApplication()) as any
  const application = res.data
  application.forEach((element: Object) => {
    const a: application = element as application
    applicationList.push(a)
  })
  return applicationList
}
export async function ReleaseCourse(course: any) {
  await releaseCourse(course)
}
export async function AddAudit(id: number, content: any) {
  await addAudit(id, content)
}
export async function GetAudit(): Promise<string[]> {
  const themeList: string[] = []
  const res = (await getAudit()) as any
  const theme = res.data
  theme.forEach((item: { name: string; id: number; prompt: number }) => {
    const a: string = item.name as string
    themeList.push(a)
  })
  return themeList
}
export async function GetAllAudit(flag: boolean): Promise<application[]> {
  const studentList: application[] = []
  const teacherList: application[] = []
  const res = (await getAllAudit()) as any
  const application = res.data
  application.forEach((element: Object) => {
    const a: application = element as application
    if (a.role == 'STUDENT' && a.status != 0) {
      studentList.push(a)
    } else if (a.role == 'TEACHER' && a.status != 0) {
      teacherList.push(a)
    }
  })
  if (flag) {
    return studentList
  } else {
    return teacherList
  }
}
export async function GetAllToDoAudit(): Promise<{studentList: application[], teacherList: application[]}> {
  const studentList: application[] = []
  const teacherList: application[] = []
  const res = (await getAllAudit()) as any
  const application = res.data
  application.forEach((element: Object) => {
    const a: application = element as application
    if (a.role == 'STUDENT' && a.status == 50) {
      studentList.push(a)
    } else if (a.role == 'TEACHER' && a.status == 50) {
      teacherList.push(a)
    }
  })
  return {studentList, teacherList}
}
export async function UpdateAuditStatus(recordId: number, status: number): Promise<void> {
  await updateAuditStatus(recordId, status)
}
export async function DeleteAudit(auditId: number) {
  await deleteAudit(auditId)
}
export async function DeleteApply(recordId: number) {
  await deleteApply(recordId)
}
export async function GetApplyResult(auditId:number) {
  const applicationList: application[] = []
  const res = (await getApplyResult(auditId)) as any
  const application = res.data
  application.forEach((element: Object) => {
    const a: application = element as application
    applicationList.push(a)
  })
  return applicationList
}
export async function GetApplyResultId(): Promise<{id1:number,id2:number,id3:number}>
{
//1，课外活动
//2，社会实践
//3，成果奖励
  let id1:number = 0,id2:number = 0,id3:number = 0
  const res = (await getAudit()) as any
  const theme = res.data
  theme.forEach((item: {name:string, id: number, prompt: number}) => {
    const a: string = item.name as string
    if (a == '课外活动') {
      id1 = item.id
    }else if (a == '社会实践') {
      id2 = item.id
    }else if (a == '成果奖励') {
      id3 = item.id
    }
  })
  console.log(id1,id2,id3)
  return {id1, id2, id3}
}
export async function GetStatistics(): Promise<{ value: number, name: string }[]> {
  const res = await getStatistics();
  const data = res.data;
  const leaveData = data['请假外出'];
  const processedData = Object.entries(leaveData).map(([name, value]) => ({
      name: name,
      value: value as number
  }));
  return processedData;
}
export async function GetOtherStatistics(): Promise<{ pratice: number[], activity: number[] }> {
  const res = await getStatistics();
  const data = res.data;
  const practiceData = new Array(5).fill(0);
  const activityData = new Array(5).fill(0);
  const months = ['2023.9', '2023.10', '2023.11', '2023.12', '2024.1'];
  months.forEach((month, index) => {
    if (data['社会实践'][month]) {
      practiceData[index] = data['社会实践'][month];
    }
  });
  months.forEach((month, index) => {
    if (data['课外活动'][month]) {
      activityData[index] = data['课外活动'][month];
    }
  });

  return { pratice: practiceData, activity: activityData };
}